﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Download;
using PowerCreator.LMS.Download.Entity;

public partial class sysadmin_DownManager_AddDownload : PowerCreator.LMS.Web.Base.WebBasePage
{
    Download objDownload = new Download();
    public int DownloadID = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            BinderDownloadType();
            if (Request.QueryString["DownloadID"] != null)
            {
                DownloadID = int.Parse(Request.QueryString["DownloadID"].ToString());
                if (DownloadID != 0)
                {
                    ViewState["DownloadID"] = DownloadID.ToString();
                    BinderForm(DownloadID);
                }
                if (Request.QueryString["AddDownload"] != null)
                {
                    if (Request.QueryString["AddDownload"].ToString() == "upload")
                    {
                        string path = Request.QueryString["fullpath"].ToString();
                        txt_DownUrl.Text = path;
                        ViewState["filesize"] = Request.QueryString["filesize"];
                    }
                }
            }
        }
    }
    protected override void InitPage()
    {
        //lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddJob_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobName.Text = GetLanageValueByKey("lbl_AddJob_JobName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobDescription.Text = GetLanageValueByKey("lbl_AddJob_JobDescription", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        //base.InitPage();

        lbl_InfoTitle_AddDownload.Text = GetLanageValueByKey("lab_AddDownload_DownloadTypeName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_DownloadUploadInfo.Text = GetLanageValueByKey("lab_AddDownload_ResourseFileUplaod", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_DownloadFileUrl.Text = GetLanageValueByKey("lab_AddDownload_ResourseFile", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_DownloadBaseInfo.Text = GetLanageValueByKey("lab_AddDownload_ResourseInfoSet", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_DownloadName.Text = GetLanageValueByKey("lbl_ResourceList_ResourceName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_FKDownDownTypeID.Text = GetLanageValueByKey("lbl_AddResourceInfo_ResourceClass", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_DownEdition.Text = GetLanageValueByKey("lab_AddDownload_SoftVersionNumber", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddDownload_msg_DownEdition.Text = GetLanageValueByKey("msg_AddDownload_msg1", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_DownDescription.Text = GetLanageValueByKey("lab_AddDownload_ResourseDes", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_DownState.Text = GetLanageValueByKey("lab_AddDownload_ResourseState", PowerCreator.LMS.Language.LanagetItemType.Page);
        cb_DownState.Text = "";
        //lbl_DownloadPictureUrl.Text = "图片";
        //lbl_DownloadPictureUrl_intro.Text = "只有在前台页面做特殊展示，例如做图片新闻时才需上传！";

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void BinderDownloadType()
    {
        DataTable dt = new DataTable();
        DownType objType = new DownType();
        dt = objType.Query("");
        ddl_FKDownDownTypeID.DataSource = dt;
        ddl_FKDownDownTypeID.DataTextField = "DownTypeName";
        ddl_FKDownDownTypeID.DataValueField = "DownTypeID";
        ddl_FKDownDownTypeID.DataBind();

    }
    private void BinderForm(int id)
    {
        VOTDownload vo = new VOTDownload();
        vo = objDownload.Load(id);

        txt_DownUrl.Text = vo.DownUrl;
        txt_DownName.Text = vo.DownName;
        txt_DownEdition.Text = vo.DownEdition;
        txt_DownDescription.Text = vo.DownDescription;
        ddl_FKDownDownTypeID.SelectedValue = vo.FKDownDownTypeID.ToString();
        if (vo.DownState == true)
            cb_DownState.Checked = true;
    }
    private bool CheckPageData()
    {
        string search = string.Format("DownName = '{0}'", txt_DownName.Text.Trim());
        if (ViewState["DownloadID"] != null)
        {
            search += "and DownID<>" + ViewState["DownloadID"].ToString();
        }
        if (objDownload.Count(search) != 0)
        {

            //lbl_msg_Error.Text = GetLanageValueByKey("lbl_msg_addjob_aspx_count", PowerCreator.LMS.Language.LanagetItemType.MSG);

            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("msg_AddDownload_Msg2", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        if (ddl_FKDownDownTypeID.Items.Count < 1)
        {
            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("lab_ManagerDownload_msg3", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTDownload vo = new VOTDownload();
        if (ViewState["DownloadID"] != null)
        {
            vo = objDownload.Load(int.Parse(ViewState["DownloadID"].ToString()));
        }
        vo.DownUrl = txt_DownUrl.Text.Trim();
        vo.DownName = txt_DownName.Text.Trim();
        vo.DownDescription = txt_DownDescription.Text;
        vo.DownEdition = txt_DownEdition.Text.Trim();
        vo.FKDownDownTypeID = int.Parse(ddl_FKDownDownTypeID.SelectedValue);
        if (cb_DownState.Checked == true)
            vo.DownState = true;
        vo.DownSize = getFileSize();
        vo.DownUpdateTime = DateTime.Now;
        vo.DownClickNum = 0;


        PowerCreator.LMS.Web.Base.BasePage bpage = new PowerCreator.LMS.Web.Base.BasePage();
        vo.FKDownCreatorid = bpage.GetCreatedByInfo().UserID;

        vo.DownCreatorname = bpage.GetCreatedByInfo().UserName;

        if (ViewState["DownloadID"] == null)
        {
            if (objDownload.AddDownload(vo))
            {
                string url = "ManagerDownload.aspx";
                Script.RedirectURL(url);
            }
        }
        else
        {
            if (objDownload.Update(vo))
            {
                string url = "ManagerDownload.aspx";
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManagerDownload.aspx";
        Script.RedirectURL(url);
    }
    private int getFileSize()
    {
        int filesize = 0;
        if (ViewState["filesize"] != null)
        {
            int Rem;
            filesize = Math.DivRem(int.Parse(ViewState["filesize"].ToString()), 1024, out Rem);
            if (int.Parse(Rem.ToString().Substring(0, 1)) >= 5)
            {
                filesize = filesize + 1;
            }
        }
        else
            filesize = 0;
        return filesize;
    }
}
